package ltd.starriddle.smart4j.chapter2.controller;

import ltd.starriddle.smart4j.chapter2.model.Customer;
import ltd.starriddle.smart4j.chapter2.service.CustomerService;
import ltd.starriddle.smart4j.chapter2.util.CastUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 进入 客户详情 界面
 *
 * @author chen
 */
@WebServlet("/customer_show")
public class CustomerShowServlet extends HttpServlet {

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomerShowServlet.class);

    private CustomerService customerService;

    @Override
    public void init() throws ServletException {
        customerService = new CustomerService();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        long id = CastUtil.castLong(req.getParameter("id"));
        LOGGER.info("req 客户详情 id={}", id);

        Customer customer = customerService.getCustomer(id);
        req.setAttribute("customer", customer);
        LOGGER.info("resp 客户详情 customer={}", customer);

        req.getRequestDispatcher("/WEB-INF/view/customer_show.jsp").forward(req, resp);
    }
}
